package commonControllers
import (
	."LiquidMeasurement/lib"
	//"fmt"
	"strings"
	"net"
	"encoding/json"
	//"fmt"
	"github.com/astaxie/beego"
)
type UdpClientController struct {
	UdpPort     uint8

}


func SendInformBroadcast() ( error) {
	ip := GetLocalIP()
	cmd := TcpUdpCmd{200,1,"192.168.2.103"}
	out,_ := json.Marshal(cmd)
	addr_list := strings.Split(ip, ".")
	addr_list[3] = "255"
	ip = strings.Join(addr_list, ".")
	beego.Info("send udp :", string(out))
	beego.Info("ip :", ip+":10086")

	// 创建连接
	socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
		IP:   net.IPv4(192, 168, 2, 255),
		Port: 10086,
	})
	if err != nil {
		beego.Info("连接失败!", err)
		return err
	}
	defer socket.Close()

	// 发送数据
	senddata := []byte(out)
	_, err = socket.Write(senddata)
	if err != nil {
		beego.Info("发送数据失败!", err)
		return err
	}
	socket.Close()
	return err
}
